"""
Created by 饼干 on 2019/11/1 16:18
"""
from wtforms import Form, StringField, PasswordField
from wtforms.validators import Length, DataRequired, Email, ValidationError, EqualTo


from app.models.user import User

__author__ = '饼干'


class EmailForm(Form):
    email = StringField(validators=[DataRequired(), Length(8, 64),
                                    Email(message='电子邮箱不符合规范')])


class LoginForm(EmailForm):
    # email = StringField(validators=[DataRequired(), Length(8, 64),
    #                                 Email(message='电子邮箱不符合规范')])
    password = PasswordField(validators=[
        DataRequired(message='密码不能为空，请输入密码'), Length(6, 32)])




class RegisterForm(LoginForm):
    nickname = StringField(validators=[
        DataRequired(), Length(2, 10, message='昵称至少需要两个字符， 最多10个字符')])

    def validate_email(self, field):
        if User.query.filter_by(email=field.data).first():
            raise ValidationError('电子邮件已经被注册')

    def validate_nickname(self, field):
        if User.query.filter_by(nickname=field.data).first():
            raise ValidationError('用户名已经被注册')


class RestPasswordForm(Form):
    password1 = PasswordField(validators=[
        DataRequired(),
        Length(6, 32, message='密码长度至少需要在6到32个字符之间'),
        EqualTo('password2', message='两次输入的密码不相同')
    ])
    password2 = PasswordField(validators=[
        DataRequired(), Length(6, 32)])
